package com.gpj.learn.rabbitmq.topic;

import com.gpj.learn.rabbitmq.common.CommonConstants;
import com.gpj.learn.rabbitmq.common.RabbitUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @className RoutingKeyConsumer1
 * @description
 *   TODO
 * @author GPJ
 * @date 2021/5/30 12:33
 * @version 1.0
 **/
@Slf4j
public class TopicConsumer1 {
	private static final String QUEUE_ROUTING_CONSUMER = "queue-topic-consumer-1";

	public static void main(String[] args) throws IOException {
		log.info("------------- 开始消费消息 -------------");
		Channel channel = RabbitUtil.createChannel(QUEUE_ROUTING_CONSUMER);

		channel.queueBind(QUEUE_ROUTING_CONSUMER,
				CommonConstants.RABBITMQ_EXCHANGE_TOPIC, "*.*.topicA");

		channel.basicQos(1);
		RabbitUtil.consumeMessage(channel, QUEUE_ROUTING_CONSUMER, new DefaultConsumer(channel){
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
				String message = new String(body);
				log.info("[{}]接收到消息[{}]:{}", "TopicConsumer1", envelope.getDeliveryTag(), message);
				channel.basicAck(envelope.getDeliveryTag(), false);
			}
		});
	}
}
